import inquirer from "inquirer";
import chalk from "chalk";
import fse from "fs-extra";
import path from "node:path";
import { fileURLToPath } from "node:url";
import ejs from "ejs";

export const __dirname = (meta) => {
  return path.dirname(fileURLToPath(meta.url));
};

export const cwd = process.cwd();

export const TEMPLATE_DIR = path.resolve(__dirname(import.meta), "templates");

// console.log(`${chalk.green("create")} : /public/css/index.css`);
/**
 * 确认
 * @param {*} msg 问答内容
 * @returns 返回一个布尔值的 promise
 */
export const confirm = async (msg) => {
  return await inquirer
    .prompt({
      type: "confirm",
      name: "name",
      message: msg, // '目的地不为空，确认继续么？',
    })
    .then((res) => {
      return res.name;
    });
};

/**
 * 创建文件
 * @param {*} base
 * @param {*} dir
 */
export const mkdir = (dirPath) => {
  console.log(`   ${chalk.green("create")} : ${dirPath}${path.sep}`);
  // const dirPath = path.join(base, dir);
  fse.mkdirpSync(dirPath);
};

// confirm("目的地不为空，确认继续么？");
// mkdir(".", "public/javascripts");
// mkdir(".", "public/images");
// mkdir(".", "public/stylesheets");

/**
 * 拷贝文件
 * @param {*} fromDir 文件源路径
 * @param {*} toDir 输出文件路径
 */
export const copyTemplateMulti = (fromDir, toDir, nameGlob = "js") => {
  // 查询模板里的文件
  const files = fse.readdirSync(fromDir);
  const fileFilters = files.filter((h) => {
    return path.extname(h).endsWith(nameGlob);
  });
  // console.log(nameGlob + " >>> ", fileFilters);
  fileFilters.forEach((h) => {
    const fromFilePath = path.resolve(fromDir, h);
    const toFilePath = path.resolve(toDir, h);
    fse.copyFileSync(fromFilePath, toFilePath);
  });
};

export const copyTemplate = (fromFile, toFrom) => {
  fse.copyFileSync(fromFile, toFrom);
};

// copyTemplateMulti("templates/css", "public/stylesheets");

/**
 * echo str > file.
 *
 * @param {String} file
 * @param {String} str
 */

export const write = (file, str) => {
  fse.writeFileSync(file, str);
  console.log(`   ${chalk.green("create")} : ${file}`);
};

/**
 * 返回 ejs 填充后的文本
 * @param {String} fileString
 * @param {Object} data
 * @returns
 */
export const renderEjsFile = (fileString, data) => {
  return ejs.render(fileString, data);
};

/**
 * 选择模板引擎
 * @returns 返回 value 为模板引擎的 promise
 */
export const chooseEngine = async () => {
  return await inquirer
    .prompt({
      type: "list",
      name: "name",
      message: "选择模板引擎",
      choices: [
        {
          name: "ejs模板引擎",
          value: "ejs",
        },
        {
          name: "jade模板引擎",
          value: "jade",
        },
      ],
    })
    .then((res) => {
      return res.name;
    });
};

/**
 * 选择模板引擎
 * @returns 返回 value 为模板引擎的 promise
 */
export const chooseCss = async () => {
  return await inquirer
    .prompt({
      type: "list",
      name: "name",
      message: "选择css预编译方案",
      choices: [
        {
          name: "css",
          value: "css",
        },
        {
          name: "less",
          value: "less",
        },
        {
          name: "sass",
          value: "scss",
        },
      ],
    })
    .then((res) => {
      return res.name;
    });
};

export const pathExists = (path) => {
  try {
    fse.accessSync(path);
    return true;
  } catch (error) {
    return false;
  }
};
